﻿using SoundEngine3.AudioProcessing.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DefaultDspPluginsLib.Depricated
{
    public class Effect_Equalizer2
    {
        private FilterEQFast[] mEQ;
        private int mEQCount;
        private double mBand1 = 1;
        private double mBand2 = 1;
        private double mBand3 = 1;
        private double mLowFrequency;
        private double mHighFrequency;
        private int mSampleRate;

        public double Band1
        {
            get { return mBand1; }
            set
            {
                mBand1 = value;
                foreach (var eq in mEQ)
                {
                    eq.low_volyme = value;
                }
            }
        }
        public double Band2
        {
            get { return mBand2; }
            set
            {
                mBand2 = value;
                foreach (var eq in mEQ)
                {
                    eq.mid_volyme = value;
                }
            }
        }
        public double Band3
        {
            get { return mBand3; }
            set
            {
                mBand3 = value;
                foreach (var eq in mEQ)
                {
                    eq.hi_volyme = value;
                }
            }
        }
        public double LowFrequency
        {
            get { return mLowFrequency; }
            set
            {
                mLowFrequency = value;
                foreach (var eq in mEQ)
                {
                    eq.Frequency = value;
                    eq.Init();
                }
            }
        }
        public double HighFrequency
        {
            get { return mHighFrequency; }
            set
            {
                mHighFrequency = value;
                foreach (var eq in mEQ)
                {
                    eq.Q = value;
                    eq.Init();
                }
            }
        }

        public Effect_Equalizer2()
        {
            // Minimum 1 size. Otherwise the Low/HighFrequency properties will crash.
            mEQCount = 1;
            mEQ = new FilterEQFast[1];
            mEQ[0] = new FilterEQFast();
        }
        
        public void SetOutputChannels(int channels, int sampleRate)
        {
            if (mEQCount != channels || mSampleRate != sampleRate)
            {
                // Minimum 1 size allowed.
                channels = Math.Max(1, channels);

                if (mEQ.Length < channels)
                {
                    mEQ = new FilterEQFast[channels];

                    for (int i = 0; i < mEQ.Length; i++)
                    {
                        mEQ[i] = new FilterEQFast();
                    }
                }

                mEQCount = channels;
                mSampleRate = sampleRate;

                foreach (var eq in mEQ)
                {
                    eq.Reset();

                    eq.SampleRate = sampleRate;
                    eq.low_volyme = mBand1;
                    eq.mid_volyme = mBand2;
                    eq.hi_volyme = mBand3;
                    eq.Frequency = mLowFrequency;
                    eq.Q = mHighFrequency;
                    eq.Init();
                }
            }
        }

        internal void Update(int sampleRate, int outputChannels, double lowFrequency, double highFrequency, double band1, double band2, double band3)
        {
            SetOutputChannels(outputChannels, sampleRate);
            LowFrequency = lowFrequency;
            HighFrequency = highFrequency;
            Band1 = band1;
            Band2 = band2;
            Band3 = band3;
        }

        public void Process(ref ZquenceStudio.DefaultDspPlugins.Effects.Equalizer2.Equalizer2Data data, double[] audioBufferLeft, double[] audioBufferRight, double[] audioBufferLeft_output, double[] audioBufferRight_output)
        {
            for (int c = 0; c < data.Channels; c++)
            {
                double[] inputSamples = c == 0 ? audioBufferLeft : audioBufferRight;
                double[] outputSamples = c == 0 ? audioBufferLeft_output : audioBufferRight_output;
                FilterEQFast eq = mEQ[c];

                mEQ[c].ProcessTime(inputSamples, outputSamples);
            }
        }
    }
}
